package com.nb.nbbase2.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nb.nbbase2.entity.NsUser;
import com.nb.nbbase2.entity.SysArea;
import com.nb.nbbase2.model.Result;
import com.nb.nbbase2.model.dto.LoginDTO;
import com.nb.nbbase2.model.dto.RegistryDTO;
import com.nb.nbbase2.model.dto.UserQueryDTO;
import com.nb.nbbase2.model.vo.ProvinceCntVO;
import com.nb.nbbase2.model.vo.UserInfoVO;
import com.nb.nbbase2.service.INsUserService;
import com.nb.nbbase2.service.ISysAreaService;
import com.nb.nbbase2.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 作者
 * @since 2024-10-02
 */
@RestController
@RequestMapping("/nsUser")
public class NsUserController {

    @Autowired
    ISysAreaService areaService;

    @Autowired
    INsUserService nsUserService;

    @PostMapping("queryByArea")
    public Result<List<NsUser>> queryByArea(@RequestBody UserQueryDTO queryDTO){
        LambdaQueryWrapper<NsUser> wrapper = new LambdaQueryWrapper<>();
        if(Objects.nonNull(queryDTO.getDistrictId())){
            wrapper.eq(NsUser::getDistrictId,queryDTO.getDistrictId());
        }else if(Objects.nonNull(queryDTO.getCityId())){
            wrapper.eq(NsUser::getCityId,queryDTO.getCityId());
        }else if(Objects.nonNull(queryDTO.getProvinceId())){
            wrapper.eq(NsUser::getProvinceId,queryDTO.getProvinceId());
        }else{
            wrapper.last(" limit 30");
        }
        List<NsUser> list = nsUserService.list(wrapper);
        return Result.OK(list);
    }

    @PostMapping("login")
    public Result<UserInfoVO> login(LoginDTO loginDTO){
        UserInfoVO userInfoVO = nsUserService.login(loginDTO);
        return Result.OK(userInfoVO);
    }

    @PostMapping("register")
    public Result<Void> register(RegistryDTO registryDTO){
        nsUserService.registry(registryDTO);
        return Result.OK();
    }

    @PostMapping("checkQq")
    public Result<Map<String,Boolean>> checkQq(NsUser user){
        String qq = user.getQq();
        boolean flag = nsUserService.checkQqExist(qq);
        Map<String,Boolean> map = new HashMap<>();
        map.put("exist",flag);
        return Result.OK(map);
    }



    @GetMapping("provinceCnt")
    public Result<List<ProvinceCntVO>> provinceCnt(){
        List<ProvinceCntVO> list = nsUserService.countByProvince();
        return Result.OK(list);
    }


    @GetMapping("userInfo")
    public Result<NsUser> userInfo(@RequestParam String jwt){
        String userId = JwtUtil.getUserId(jwt);
        NsUser user = nsUserService.getByQQ(userId);
        return Result.OK(user);
    }
}

